package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.service.UserRoleService;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    SysMenuMapper menuMapper;

    @Autowired
    SysRoleMapper roleMapper;

    @Override
    public List<SysMenu> selectMenusByUserId(Long userId,Long roleId) {
        // 1. 查询用户所有角色
        List<SysRole> roles = roleMapper.selectRolePermissionByUserId(userId);

        boolean hasRole = roles.stream()
                .anyMatch(role -> role.getRoleId().equals(roleId));

        if (!hasRole) {
            return  null;
        }
        return menuMapper.selectMenuTreeByRoleId(roleId);
    }


}
